//:C06:CalcInventory.cpp
// More use of for_each().
#include <algorithm>
#include <ctime>
#include <iterator>
#include <ostream>
#include <vector>
#include "Inventory.h"
#include "PrintSequence.h"
using namespace std;

// To calculate inventory totals:
class InvAccum
{
    int quantity;
    int value;
    public:
        InvAccum() : quantity(0), value(0){};
        void operator()(const Inventory& inv)
        {
            quantity += inv.getQuantity();
            value += inv.getQuantity() * inv.getValue();
        }
        friend ostream&
        operator<<(ostream& os, const InvAccum& ia)
        {
            return os << "total quantity: " << ia.quantity
                      << ", total value: " << ia.value;
        }
};

int main(void)
{
    vector<Inventory> vi;
    srand(time(0));
    generate_n(back_inserter(vi), 15, InvenGen());
    print(vi.begin(), vi.end(), "vi");
    InvAccum ia = for_each(vi.begin(), vi.begin(), InvAccum());
    cout << ia << endl;
}